<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="tiles" uri="http://tiles.apache.org/tags-tiles"%>
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>

<div class="page-header">
	<h2><spring:message code="customers.search.title" /></h2>
	<spring:message code="customers.search.title.explain" />
</div>

<form:form commandName="searchCustomer" cssClass="well form-horizontal">
	<c:set var="globalErrors"><form:errors path=""/></c:set>
	<c:if test="${not empty globalErrors}">
		<div class="alert alert-error">
			<form:errors path=""/>
		</div>
	</c:if>
	
	<!-- id -->
	<c:set var="idErrors"><form:errors path="id"/></c:set>
	<div class="control-group <c:if test="${not empty idErrors}">error</c:if>">
	   	<label class="control-label"><spring:message code="customers.search.field.id" /></label>
	   	<div class="controls">
	   		<input name="id" value='<c:out value="${searchCustomer.id}"/>' placeholder="<spring:message code="customers.search.field.id" />" />
	   		<form:errors path="id" cssClass="help-inline"/>
   		</div>
	</div>
	
	<spring:message code="customers.search.or" />
	
	<!-- name -->
	<c:set var="nameErrors"><form:errors path="name"/></c:set>
	<div class="control-group <c:if test="${not empty nameErrors}">error</c:if>">
	   	<label class="control-label"><spring:message code="customers.search.field.name" /></label>
	   	<div class="controls">
	   		<input name="name" value='<c:out value="${searchCustomer.name}"/>' placeholder="<spring:message code="customers.search.field.name" />" />
	   		<form:errors path="name" cssClass="help-inline"/>
	   	</div>
	</div>
	
	<c:if test="${not empty agencies}">
	<!-- agency -->
	<c:set var="agencyErrors"><form:errors path="agencyId"/></c:set>
	<div class="control-group <c:if test="${not empty agencyErrors}">error</c:if>">
	   	<label class="control-label"><spring:message code="customers.search.field.agency" /></label>
	   	<div class="controls">
			<select name="agencyId">
				<option value=""><spring:message code="common.select.default" /></option>
				<c:forEach var="agency" items="${agencies}" >
			    	<option value="${agency.id}" <c:if test="${searchCustomer.agencyId eq agency.id}">selected="selected"</c:if> >${agency.name}</option>
	    		</c:forEach>
			</select> 
	   		<form:errors path="agencyId" cssClass="help-inline"/>
	   	</div>
	</div>
	</c:if>
	
	
	<div class="form-actions">
		<button class="btn btn-primary" type="submit"><spring:message code="common.btn.search" /></button>
		<sec:authorize access="hasRole('PERM_CREATE_CUSTOMER')">
			<button class="btn btn-info" type="button" onclick="document.location.href='<spring:url value="/customers/create"/>'"><spring:message code="common.btn.create.customer" /></button>
		</sec:authorize>
	</div>
	
</form:form>

<hr/>

<tiles:insertAttribute name="result" />
